"""
_*_ : utf-8 _*_ 
@Time: 2023/11/13 22:09
@Author: LWK
@File: 079_selenium元素定位
@Project: demo01
"""
# 1.导入selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 2.创建浏览器操作对象（参数是避免浏览器自动关闭）
options = webdriver.ChromeOptions()
options.add_experimental_option('detach', True)
browser = webdriver.Chrome(options=options)

url = 'https://www.baidu.com'
browser.get(url)

time.sleep(1)

# 获取文本框的对象
input = browser.find_element(by=By.ID, value='kw')

# 在文本框输入周杰伦
input.send_keys('周杰伦')

time.sleep(1)

# 获取百度一下的按钮
button = browser.find_element(by=By.ID, value='su')

# 点击按钮
button.click()

time.sleep(1)

# 划到浏览器页面底部
js_bottom = 'document.documentElement.scrollTop=100000'
browser.execute_script(js_bottom)

time.sleep(1)

# 获取下一页的按钮
next = browser.find_element(by=By.XPATH, value='//a[@class="n"]')

time.sleep(1)

# 点击下一页
next.click()

time.sleep(2)

# 回到上一页(后退)
browser.back()

time.sleep(2)

# 前进
browser.forward()

time.sleep(2)

# 退出
browser.quit()